package edu.ustb.railwayticket.controller;

import edu.ustb.railwayticket.entity.Ticket;
import edu.ustb.railwayticket.service.TicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;

@CrossOrigin
@Api(tags = "购票接口")
@RestController
@RequestMapping("/api/v1/ticket")
public class TicketController {

    private Ticket ticket;
    @Autowired
    private TicketService ticketService;
    private Integer seatNumber = 1;

    @ApiOperation("helloworld")
    @GetMapping(value = "/hello")
    public String hello(String name) {
        String res = "Baka " + name;
        return res;
    }

    @ApiOperation("订票服务")
    @PostMapping("/order")
    public Ticket order(Integer passengerNum, String departureTime, String departure, String destination) throws ParseException {
        Date date = new Date();
        ticket = ticketService.order(passengerNum, departureTime, departure, destination);
        if(ticket.getTicketNumber() == null)return null;
        seatNumber += 1;
        return ticket;
    }

    @ApiOperation("取消订票")
    @GetMapping("/refund")
    public String refund(Integer passengerNum) {
        String result = ticketService.refund(passengerNum);
        return result;
    }

    @ApiOperation("改签")
    @PostMapping("/changeTime")
    public String change(Integer passengerNum, String departureTime) {
        String result = ticketService.changeTime(passengerNum, departureTime);
        return result;
    }

    @ApiOperation("根据票据编号查询")
    @GetMapping("/query")
    public Ticket query(Integer ticketNum) {
        ticket = ticketService.queryTicket(ticketNum);
        return ticket;
    }

    @ApiOperation("根据乘客编号查询")
    @GetMapping("/querypassenger")
    public Ticket queryPassenger(Integer passengerNum) throws ParseException {
        ticket = ticketService.queryPassenger(passengerNum);
        return ticket;
    }

}
